package com.wolfalpha.utron.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.umeng.analytics.MobclickAgent;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;

/**
 * 用户协议界面
 */
public class AgreementActivity extends BaseNormalActivity {

    private WebView wbAgree;            //网页视图
    private ProgressDialog pd;          //进度条

    private static final int PD_SHOW = 1;
    private static final int PD_STOP = 2;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case PD_SHOW:
                    //显示进度条
                    if (pd != null && pd.isShowing()) {
                    } else {
                        pd = new ProgressDialog(AgreementActivity.this);
                        pd.setMessage("正在加载");
                        pd.setCancelable(false);
                        pd.show();
                    }
                    break;
                case PD_STOP:
                    //关闭进度条
                    pd.dismiss();
                    pd = null;
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agreement);

        setTvTitle("用户协议");
        wbAgree = (WebView) findViewById(R.id.wb_agreement);
        loadData();
    }

    /**
     * 加载网页数据
     */
    private void loadData() {
        wbAgree.loadUrl("http://139.196.17.95/Utron/Static/user_agreement.html");
        wbAgree.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                //加载完成
                if (pd.isShowing()) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } finally {
                                handler.sendEmptyMessage(PD_STOP);
                            }
                        }
                    }).start();
                }
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                //加载开始
                handler.sendEmptyMessage(PD_SHOW);
                super.onPageStarted(view, url, favicon);
            }
        });
    }

}
